import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:interview/components/status_bar_warpper.dart';
import 'package:interview/controller/navigation_controller.dart';
import 'package:interview/controller/search_history_controller.dart';
import 'package:interview/pages/account/components/input_item.dart';
import 'package:interview/pages/search/components/search_header.dart';
import 'package:interview/pages/search/components/search_history.dart';
import 'package:interview/pages/search/components/search_result.dart';
import 'package:interview/theme/app_colors.dart';

class SearchPage extends StatefulWidget {
  final String? tabKeyword;
  const SearchPage({super.key , this.tabKeyword});
  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  final navigation = Get.find<NavigationController>();
  final searchHistoryController = Get.put(SearchHistoryController());
  final GlobalKey<InputItemState> inputItemKey = GlobalKey<InputItemState>();


  String inputValue = "" ; // 当前输入框文字
  String searchKeyword = ""; // 当前触发搜索的关键词

  /// 通用搜索触发方法
  void onSearch(String keyword) {
    if (keyword.trim().isEmpty) return;
    // 更新搜索历史
    searchHistoryController.addSearchKeyword(keyword);
    // 更新搜索状态并触发刷新
    setState(() {
      searchKeyword = keyword; // 更新关键词
    });
  }

  /// 点击历史记录的逻辑
  void onHistoryKeywordTap(String historyKeyword) {
    inputItemKey.currentState?.setValue(historyKeyword);
    onSearch(historyKeyword); // 点击历史记录等同于触发搜索
  }

  /// 输入框提交搜索
  void onSearchSubmit(String keyword) {
    onSearch(keyword); // 输入框提交时触发搜索
  }

  /// 点击搜索
  void inputValueSubmit(){
    onSearchSubmit(inputValue);
  }

  void onChanged (String value){
    setState(() {
      inputValue = value;
    });
    if(inputValue.isEmpty){
      setState(() {
        searchKeyword = ""; // 清空输入框时，清空搜索内容
      });
    }
  }

  void searchKeywordClear () {
    setState(() {
      searchKeyword = ""; 
    });
  }

  @override
  void initState() {
    super.initState();
    if( widget.tabKeyword != null){
      onHistoryKeywordTap(widget.tabKeyword!);
    }
  }

  @override
  void dispose() {
    inputItemKey.currentState?.clear(); // 清空输入框状态
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).extension<AppColors>()!;
    return StatusBarWrapper(
      statusBarColor: theme.subjectPure!,
      statusBarIconBrightness: Theme.of(context).brightness == Brightness.dark? Brightness.light: Brightness.dark,
      child: Scaffold(
        appBar: null,
        resizeToAvoidBottomInset: false, // 禁止因键盘弹出而调整布局
        body: SafeArea(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 12),
            color: theme.subjectPure,
            child: Column(
              children: [
                SearchHeader(
                  theme: theme, 
                  inputItemKey: inputItemKey, 
                  searchKeyword: searchKeyword, 
                  onChanged: onChanged, 
                  searchKeywordClear: searchKeywordClear, 
                  onSearchSubmit: onSearchSubmit, 
                  inputValueSubmit: inputValueSubmit
                ),
                if (searchKeyword.isEmpty) 
                  Expanded(child: 
                    SearchHistory( 
                      theme: theme , 
                      onHistoryKeywordTap:onHistoryKeywordTap,
                      searchHistoryController: Get.find<SearchHistoryController>(),
                    )
                  ),
                if (searchKeyword.isNotEmpty)
                  Expanded(child: 
                    SearchResult(
                      theme: theme, 
                      searchKeyword: searchKeyword,
                      initCurrentIndex: widget.tabKeyword == null ? 0 : 1,
                    )
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
